package com.vr.cloudpicture.controller;

import com.vr.cloudpicture.common.BaseResponse;
import com.vr.cloudpicture.common.ResultUtils;
import com.vr.cloudpicture.exception.ErrorCode;
import com.vr.cloudpicture.exception.ThrowUtils;
import com.vr.cloudpicture.manager.auth.annotation.SaSpaceCheckPermission;
import com.vr.cloudpicture.manager.auth.model.SpaceUserPermissionConstant;
import com.vr.cloudpicture.model.dto.DeleteRequest;
import com.vr.cloudpicture.model.dto.spaceuser.SpaceUserAddRequest;
import com.vr.cloudpicture.model.dto.spaceuser.SpaceUserEditRequest;
import com.vr.cloudpicture.model.dto.spaceuser.SpaceUserQueryRequest;
import com.vr.cloudpicture.model.dto.user.UserLogin;
import com.vr.cloudpicture.model.entity.SpaceUser;
import com.vr.cloudpicture.model.vo.SpaceUserVO;
import com.vr.cloudpicture.service.ISpaceUserService;
import com.vr.cloudpicture.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 空间用户控制器
 *
 * @author hzh
 * @date 2025/05/18
 */
@Slf4j
@RestController()
@RequestMapping("/space-user")
@RequiredArgsConstructor
public class SpaceUserController {
    private final ISpaceUserService spaceUserService;
    private final IUserService userService;


    @ApiOperation(value = "查询某成员的空间信息")
    @PostMapping("/get")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGER)
    public BaseResponse<SpaceUser> getSpaceUser(@RequestBody SpaceUserQueryRequest spaceUserQueryRequest) {
        return ResultUtils.success(spaceUserService.getSpaceUser(spaceUserQueryRequest));
    }


    @ApiOperation(value = "获取空间的成员信息列表,脱敏后")
    @PostMapping("/list")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGER)
    public BaseResponse<List<SpaceUserVO>> listSpaceUserVO(@RequestBody SpaceUserQueryRequest spaceUserQueryRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success( spaceUserService.listSpaceUserVO(spaceUserQueryRequest));
    }
    @ApiOperation(value = "编辑成员信息")
    @PostMapping("/edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGER)
    public BaseResponse<Boolean> editSpaceUser(@RequestBody SpaceUserEditRequest spaceUserEditRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(spaceUserService.editSpaceUser(spaceUserEditRequest, loginUser));
    }

    @ApiOperation(value = "给团队空间添加成员")
    @PostMapping("/add")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGER)
    public BaseResponse<Long> addSpaceUser(@RequestBody SpaceUserAddRequest spaceUserAddRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(spaceUserService.addSpaceUser(spaceUserAddRequest, loginUser));
    }
    @ApiOperation(value = "删除团队空间指定成员")
    @PostMapping("/delete")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGER)
    public BaseResponse<Boolean> deleteSpaceUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(spaceUserService.deleteSpaceUser(deleteRequest, loginUser));
    }
    @ApiOperation(value = "获取当前用户加入的空间列表")
    @GetMapping("/list/my")
    public BaseResponse<List<SpaceUserVO>> listMyTeamSpaceUser(HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        SpaceUserQueryRequest spaceUserQueryRequest = new SpaceUserQueryRequest();
        spaceUserQueryRequest.setUserId(loginUser.getId());
        return ResultUtils.success(spaceUserService.listSpaceUserVO(spaceUserQueryRequest));
    }
}
